Instalação: pip install fbprophet
Seasonality
Changing growth rate
Special days
In [1]:
import matplotlib
matplotlib.use('TkAgg')
In [2]:
%pylab inline
pylab.rcParams['figure.figsize'] = (14, 9)
In [3]:
import pandas as pd
import numpy as np
from fbprophet import Prophet
In [4]:
df = pd.read_csv('https://raw.githubusercontent.com/facebookincubator/prophet/master/examples/example_wp_peyton_manning.csv')
df['y'] = np.log(df['y'])
df.head()
Out[4]:
In [5]:
m = Prophet()
m.fit(df)
Out[5]:
In [6]:
future = m.make_future_dataframe(periods=365)
future.tail()
Out[6]:
In [7]:
forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
Out[7]:
In [8]:
m.plot(forecast)
Out[8]:
In [9]:
m.plot_components(forecast)
Out[9]:
Forecasting Growth
In [10]:
df = pd.read_csv('https://raw.githubusercontent.com/facebookincubator/prophet/master/examples/example_wp_R.csv')
import numpy as np
df['y'] = np.log(df['y'])
In [11]:
df['cap'] = 8.5
In [18]:
m = Prophet(growth='logistic')
m.fit(df)
Out[18]:
In [19]:
future = m.make_future_dataframe(periods=1826)
future['cap'] = 8.5
fcst = m.predict(future)
m.plot(fcst);
In [14]:
m = Prophet(growth='linear')
m.fit(df)
Out[14]:
In [15]:
future = m.make_future_dataframe(periods=1826)
future['cap'] = 8.5
fcst = m.predict(future)
m.plot(fcst);
Trend Changepoints
In [20]:
m = Prophet(changepoint_prior_scale=0.5)
forecast = m.fit(df).predict(future)
m.plot(forecast);
In [21]:
m = Prophet(changepoint_prior_scale=0.001)
forecast = m.fit(df).predict(future)
m.plot(forecast);
In [22]:
m = Prophet(changepoints=['2014-01-01'])
forecast = m.fit(df).predict(future)
m.plot(forecast);
In [23]:
playoffs = pd.DataFrame({
'holiday': 'playoff',
'ds': pd.to_datetime(['2008-01-13', '2009-01-03', '2010-01-16',
'2010-01-24', '2010-02-07', '2011-01-08',
'2013-01-12', '2014-01-12', '2014-01-19',
'2014-02-02', '2015-01-11', '2016-01-17',
'2016-01-24', '2016-02-07']),
'lower_window': 0,
'upper_window': 1,
})
superbowls = pd.DataFrame({
'holiday': 'superbowl',
'ds': pd.to_datetime(['2010-02-07', '2014-02-02', '2016-02-07']),
'lower_window': 0,
'upper_window': 1,
})
holidays = pd.concat((playoffs, superbowls))
In [24]:
m = Prophet(holidays=holidays)
forecast = m.fit(df).predict(future)
In [25]:
forecast[(forecast['playoff'] + forecast['superbowl']).abs() > 0][
['ds', 'playoff', 'superbowl']][-10:]
Out[25]:
In [26]:
m.plot_components(forecast);
In [27]:
m = Prophet(holidays=holidays, holidays_prior_scale=1).fit(df)
forecast = m.predict(future)
forecast[(forecast['playoff'] + forecast['superbowl']).abs() > 0][
['ds', 'playoff', 'superbowl']][-10:]
Out[27]:
Uncertainty Intervals
In [28]:
forecast = Prophet(interval_width=0.95).fit(df).predict(future)
In [ ]:
m = Prophet(mcmc_samples=500)
forecast = m.fit(df).predict(future)
In [ ]:
m.plot_components(forecast);
Outliers
In [ ]:
df = pd.read_csv('https://raw.githubusercontent.com/facebookincubator/prophet/master/examples/example_wp_R_outliers1.csv')
df['y'] = np.log(df['y'])
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=1096)
forecast = m.predict(future)
m.plot(forecast);
In [ ]:
df.loc[(df['ds'] > '2010-01-01') & (df['ds'] < '2011-01-01'), 'y'] = None
model = Prophet().fit(df)
model.plot(model.predict(future));
In [ ]:
df = pd.read_csv('https://raw.githubusercontent.com/facebookincubator/prophet/master/examples/example_wp_R_outliers2.csv')
df['y'] = np.log(df['y'])
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=1096)
forecast = m.predict(future)
m.plot(forecast);
In [ ]:
df.loc[(df['ds'] > '2015-06-01') & (df['ds'] < '2015-06-30'), 'y'] = None
m = Prophet().fit(df)
m.plot(m.predict(future));
In [ ]:
df = pd.read_csv('https://raw.githubusercontent.com/facebookincubator/prophet/master/examples/example_retail_sales.csv')
m = Prophet().fit(df)
future = m.make_future_dataframe(periods=3652)
fcst = m.predict(future)
m.plot(fcst);
In [ ]:
future = m.make_future_dataframe(periods=120, freq='M')
fcst = m.predict(future)
m.plot(fcst);
In [ ]: